package com.dcxm.stu.controller;

import com.dcxm.stu.bean.Message;
import com.dcxm.stu.bean.Student;
import com.dcxm.stu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/student")
public class StudentController {


    private Student student = new Student();

    private final StudentService studentServiceI;

    @Autowired
    public StudentController(StudentService studentServiceI) {
        this.studentServiceI = studentServiceI;
    }


    /**
     * /student/login
     *
     * @param request 前端请求
     * @return 返回loginInfo的json格式
     */
    @RequestMapping(value = "/login")
    public Student login(HttpServletRequest request) {
        student.setTelphone(request.getParameter("telphone"));
        student.setUsername(request.getParameter("username"));
        student.setPassword(request.getParameter("password"));
        return studentServiceI.login(student);
    }

    /**
     * 注册信息
     *
     * @param request 请求
     * @return message的json串
     */
    @RequestMapping(value = "/register")
    public Message register(HttpServletRequest request) {
        student.setUsername(request.getParameter("username"));
        student.setTelphone(request.getParameter("telphone"));
        student.setPassword(request.getParameter("password"));
        return studentServiceI.register(student);
    }

    /**
     * @param request 前端请求
     * @return message 包含是否成功
     */
    @RequestMapping(value = "/updateInfo")
    public Message updateInfo(HttpServletRequest request) {
        student.setTelphone(request.getParameter("telphone"));
        student.setUsername(request.getParameter("username"));
        student.setPassword(request.getParameter("password"));
        return studentServiceI.updateInfo(student);
    }

    /**
     * 图片的上传
     *
     * @param request 请求
     * @param file    要上传的文件
     */
    @RequestMapping(value = "/up")
    public Message upload(@RequestParam MultipartFile file, HttpServletRequest request) {
        String telphone = request.getParameter("telphone");
        String path = request.getSession().getServletContext().getRealPath("/upload/");
        return studentServiceI.upImg(file, telphone, path);
    }
}
